package org.umlviewer.paint;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.umlviewer.model.DiagramElement;
import org.umlviewer.model.UmlDiagram;

/**
 *
 * @author Rafa
 */
//Jpanel que dibuja el diagrama asociado
public class UMLDiagramViewer extends JPanel{

    UmlDiagram diagram=null;
    private int gridsize = 10;
    private int numfils=0, numcols=0;
    private boolean printgrid;
    private boolean adjustgrid;
    private int i,j;
    private double scale = 1.0;
    private DiagramViewerMouseAdapter mouseAdpater;
    //para pintar la linea mientras se crea una relacion
    private int pfx,pfy;
    private int pix,piy;
    //para mostrar los joption y abrir los jdialog
    private JFrame parentframe;
    
    public UMLDiagramViewer(){
        super();
        diagram = new UmlDiagram();
        mouseAdpater = new DiagramViewerMouseAdapter(this);
        this.addMouseListener(mouseAdpater);
        this.addMouseMotionListener(mouseAdpater);
        parentframe = null;
    }
    
    public UMLDiagramViewer(JFrame parent){
        this();
        this.parentframe = parent;
    }
    
    //se sobrescribe este metodo para evitar el pestaneo al mover objetos
    public void paintComponent(Graphics g2)
    {     
        Graphics2D g = (Graphics2D) g2;   
        g.scale(scale, scale);
        super.paintComponent(g);
        if(printgrid){
            numfils = getWidth()/gridsize;
            numcols = getHeight()/gridsize;
            for(i=1; i<= numfils; i++)
                for(j=1; j<=numcols; j++)
                    g.drawLine(i*gridsize, j*gridsize, i*gridsize, j*gridsize);
        }
        if(diagram!=null)
            diagram.paint(g);
        //la linea mientras se pinta una relacion
        g.drawLine(pix, piy, pfx, pfy);
    }

    public JFrame getParentFrame() {
        return parentframe;
    }

    public void setParentFrame(JFrame parentframe) {
        this.parentframe = parentframe;
    }
        

    public UmlDiagram getDiagram() {
        return diagram;
    }

    public void setDiagram(UmlDiagram diagram) {        
        this.diagram = diagram;
        mouseAdpater.setDiagram(diagram);
    }

    public boolean isPrintGrid() {
        return printgrid;
    }
    
    public boolean isAdjustGrid(){
        return adjustgrid;
    }

    public void setPrintGrid(boolean printgrid) {
        this.printgrid = printgrid;
    }
    
    public void setAdjustGrid(boolean adjustgrid){
        this.adjustgrid = adjustgrid;
    }
    
    public void setGridSize(int gridsize){
        this.gridsize = gridsize;
    }
    
    public int getGridSize(){
        return this.gridsize;
    }
        
    public void setScale(double s){
        this.scale = s;
    }
    
    public double getScale(){
        return this.scale;
    }
    //accion del mouse para agregar elementos
    public void setMouseAction(int action){
        mouseAdpater.setActualAction(action);
    }
    //para pintar las realaciones
    public void setPrintRelation(boolean b){
        mouseAdpater.setPrintRel(b);
    }
    //el tipo de relacion que se crea
    public void setRelationshipType(int type){
        if(mouseAdpater !=null)
            mouseAdpater.setRelType(type);
    }
    
    public DiagramElement getCloneableElement(){
        if(mouseAdpater!=null)
            return mouseAdpater.getCloneableElement();
        return null;
    }

    public void setPfx(int pfx) {
        this.pfx = pfx;
    }

    public void setPfy(int pfy) {
        this.pfy = pfy;
    }

    public void setPix(int pix) {
        this.pix = pix;
    }

    public void setPiy(int piy) {
        this.piy = piy;
    }        
    
    //prueba del zoom
    public void zoomIn(){
        scale+=0.5;
        repaint();
    }
    
    public void zoomOut(){
        scale-=0.5;
        repaint();
    }
}
